<?php

class User
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
			$this->_db = Zend_Registry::get('db');
    }
	
	public function getModules()
	{
		if ($handle = opendir('../application/admin/controllers')) {
			/* This is the correct way to loop over the directory. */
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != "..") {					
					$file_array = explode('.', $file);
					$filename = $file_array[0];					
					$filename = preg_replace("(Controller)", '', $filename);
					//$filename = preg_replace("(Index)", '', $filename);					
					$modules[] = $filename;
					sort($modules);										
				}
			}

			closedir($handle);
		}
		
		return $modules;
	}
	
	public function viewUsers()
	{
		//$user_level = Zend_Auth::getInstance()->getStorage()->read()->id_member;
		$getUsers = $this->_db->fetchAll("
			SELECT userid, firstname, lastname, nickname, password, email_address, date_created, is_login, status
			FROM user
			WHERE userlevel != 'superadmin'
		");
		
		return $getUsers;
	}
	
	public function addUser($form_values, $user_type)
	{
		$firstname = $form_values['firstname'];
		$lastname = $form_values['lastname']; 
		$email = $form_values['email'];
		$nickname = $form_values['nickname']; 
		$password = md5($form_values['password']);	
		$status = $form_values['status'];
		try {
			$adduser = "INSERT INTO user(firstname, lastname, nickname, password, email_address, date_created, userlevel, is_login, status)
						VALUES('" . $firstname . "', '" . $lastname . "', '" . $nickname . "', '" . $password . "', '" . $email . "', now(), '" . $user_type . "', 0, " . $status . ")";
		
			$adduser_query = $this->_db->query($adduser);
			$last_insert_user_id = $this->_db->lastInsertId(); 
			
			if($adduser_query)
			{
				$module_list = '';
				foreach($form_values['chkModules'] as $module_entry)
				{
					$module_list .= $module_entry . '|';
				}
				
				$module_list = substr($module_list, '', -1);
								
				$setpermission = "INSERT INTO permission(userid, acl)
								      VALUES(" . $last_insert_user_id . ", '" . $module_list . "')";
				$this->_db->query($setpermission);				
			}
		} catch (Exception $e) {
			return false;
		} 
	}	
	
	
    
	
	
}

